package com.example.a2205douyin.fragment;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.example.a2205douyin.R;
import com.example.a2205douyin.adapter.VideoAdapter;
import com.example.a2205douyin.baseMVVM.view.BaseFragment;
import com.example.a2205douyin.entity.VideoEntity;
import com.example.a2205douyin.recommend.RecommendViewModel;
import com.example.a2205douyin.databinding.FragmentTuijianBinding;
import com.example.a2205douyin.entity.BannerEntity;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class TuijianFragment1 extends BaseFragment<FragmentTuijianBinding, RecommendViewModel> {
    List<String> imgs = new ArrayList<>();//存放图片的集合
    List<VideoEntity.DataBean> videos = new ArrayList<>();
    VideoAdapter videoAdapter;

    int page = 1;
    int size = 10;


    public TuijianFragment1() {
        // Required empty public constructor
    }


    @Override
    protected void initData() {
        viewModel.banSuccess.observe(this, new Observer<BannerEntity>() {
            @Override
            public void onChanged(BannerEntity entity) {
                if(entity.getCode()==200){
                    for (BannerEntity.DataBean datum : entity.getData()) {
                        imgs.add(datum.getImagePath());
                    }
                    binding.ban.setImages(imgs);
                    binding.ban.setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object path, ImageView imageView) {
                            Glide.with(context).load(path).into(imageView);
                        }
                    });
                    binding.ban.start();
                }
            }
        });
        viewModel.videoSuccess.observe(this, new Observer<VideoEntity>() {
            @Override
            public void onChanged(VideoEntity entity) {
                if(entity.getCode()==200){
                    videos.addAll(entity.getData());
                    videoAdapter.notifyDataSetChanged();
                }else{
                    ToastUtils.showLong(entity.getMessage());
                }
            }
        });
        viewModel.videoFail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
        viewModel.searchSucc.observe(this, new Observer<VideoEntity>() {
            @Override
            public void onChanged(VideoEntity entity) {
                if(entity.getCode()==200){
                    //先清空所有数据
                    videos.clear();
                    videos.addAll(entity.getData());
                    videoAdapter.notifyDataSetChanged();
                }else{
                    ToastUtils.showLong(entity.getMessage());
                }
            }
        });
        viewModel.searchFail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
    }

    @Override
    protected void initView() {
        viewModel.banner();

        videoAdapter = new VideoAdapter(R.layout.item_video,videos);
        binding.rv.setAdapter(videoAdapter);
        binding.rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));

        viewModel.video(page,size);

        binding.srl.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                viewModel.video(page,size);
                binding.srl.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                //把之前的数据清空
                videos.clear();
                viewModel.video(page,size);
                binding.srl.finishRefresh();
            }
        });
        binding.search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(binding.search.getText().toString().isEmpty()){
                    videos.clear();
                    page=1;
                    viewModel.video(page,size);
                }else{
                    viewModel.search(binding.search.getText().toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    protected Class<RecommendViewModel> getViewModel() {
        return RecommendViewModel.class;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_tuijian;
    }
}